﻿using System;
using System.Collections.Generic;
using Layers;

namespace Gaming
{
    public class PlayerList
    {
        private IList<Player> _impl = Factory.NewList<Player>();
        private int _firstIndex;

        public int PositionOf(Player player)
        {
            int pos = _impl.IndexOf(player)-_firstIndex;
            if (pos < 0)
                pos += Count;
            return pos;
        }

        public Player SeekPlayer(Player curr, int dir, bool activeOnly)
        {
            int i = _impl.IndexOf(curr);
            if (i < 0)
                return null;
            int step = 0;
            while (step++<Count)
            {
                i += dir;
                if (i > Count)
                    i -= Count;
                if (i < 0)
                    i += Count;
                Player next = _impl[i];
                if (null != next)
                    if (!activeOnly)
                        return next;
                    else if (next.IsActive)
                        return next;
            }
            return null;
        }

        public int Count { get { return _impl.Count; } }
    }

    public static class PlayerState
    {
        public static int Folded = 0x01;
    }

    /// <summary>
    /// Represents player state
    /// Contains player current state flags (Folded, Active, SmallBlind, BigBlind, Button)
    /// Contains player position (0=SB, 1=BB,...,6=Button)
    /// </summary>

    public class Player
    {
        private string _id = "(empty)";
        private BitSet _state = new BitSet();
        private PlayerList _playerList = new PlayerList();


        public Player()
        {
        }

        public string ID { get { return _id; } set { _id = value; } }

        public bool IsActive { get { return !_state.Get(PlayerState.Folded); } }

        public int Position { get { return _playerList.PositionOf(this); } }
    }
}